#------------- User Settings -------------

visible CTR1 = 100  #define CT ratio for winding 1
visible CTR2 = 1000 #define CT ratio for winding 2

visible W1CTC = 0 #define compensation matrix setting for winding 1
visible W2CTC = 1 #define compensation matrix setting for winding 2

visible TAP1 = 2.09 #define TAP setting for winding 1
visible TAP2 = 2.09 #define TAP setting for winding 2

visible W1CT = 0 #set to 0 for wye and 1 for delta
visible W2CT = 0 #set to 0 for wye and 1 for delta

#------------ Definitions ------------


CT1CONFIG = IF(W1CT=0, 1, sqrt(3))
CT2CONFIG = IF(W2CT=0, 1, sqrt(3))

IAW1.phasor = IAW1.phasor*CT1CONFIG
IBW1.phasor = IBW1.phasor*CT1CONFIG 
ICW1.phasor = ICW1.phasor*CT1CONFIG
IAW2.phasor = IAW2.phasor*CT2CONFIG 
IBW2.phasor = IBW2.phasor*CT2CONFIG 
ICW2.phasor = ICW2.phasor*CT2CONFIG

mw1=W1CTC
mw2=W2CTC

#WINDING 1

scale1 = (mw1=0)*1 + ((mw1=1)+(mw1=3)+(mw1=5)+(mw1=7)+(mw1=9)+(mw1=11))*(1/sqrt(3)) + ((mw1=2)+(mw1=4)+(mw1=6)+(mw1=8)+(mw1=10)+(mw1=12))*(1/3)

I1W1_coeff1 = ((-2)*(mw1=6)) + ((-1)*((mw1=4)+(mw1=5)+(mw1=7)+(mw1=8))) + ((1)*((mw1=0)+(mw1=1)+(mw1=2)+(mw1=10)+(mw1=11))) + ((2)*(mw1=12))
I1W1_coeff2 = ((-2)*(mw1=2)) + ((-1)*((mw1=1)+(mw1=3)+(mw1=4)+(mw1=12))) + ((1)*((mw1=6)+(mw1=7)+(mw1=9)+(mw1=10))) + ((2)*(mw1=8))
I1W1_coeff3 = ((-2)*(mw1=10)) + ((-1)*((mw1=8)+(mw1=9)+(mw1=11)+(mw1=12))) + ((1)*((mw1=2)+(mw1=3)+(mw1=5)+(mw1=6))) + ((2)*(mw1=4))
I1W1C = scale1*(I1W1_coeff1*IAW1.phasor+I1W1_coeff2*IBW1.phasor+I1W1_coeff3*ICW1.phasor)*(1/(TAP1*CTR1)) 

I2W1_coeff1 = ((-2)*(mw1=10)) + ((-1)*((mw1=8)+(mw1=9)+(mw1=11)+(mw1=12))) + ((1)*((mw1=2)+(mw1=3)+(mw1=5)+(mw1=6))) + ((2)*(mw1=4))
I2W1_coeff2 = ((-2)*(mw1=6)) + ((-1)*((mw1=4)+(mw1=5)+(mw1=7)+(mw1=8))) + ((1)*((mw1=0)+(mw1=1)+(mw1=2)+(mw1=10)+(mw1=11))) + ((2)*(mw1=12))
I2W1_coeff3 = ((-2)*(mw1=2)) + ((-1)*((mw1=1)+(mw1=3)+(mw1=4)+(mw1=12))) + ((1)*((mw1=6)+(mw1=7)+(mw1=9)+(mw1=10))) + ((2)*(mw1=8))
I2W1C = scale1*(I2W1_coeff1*IAW1.phasor+I2W1_coeff2*IBW1.phasor+I2W1_coeff3*ICW1.phasor)*(1/(TAP1*CTR1)) 

I3W1_coeff1 = ((-2)*(mw1=2)) + ((-1)*((mw1=1)+(mw1=3)+(mw1=4)+(mw1=12))) + ((1)*((mw1=6)+(mw1=7)+(mw1=9)+(mw1=10))) + ((2)*(mw1=8))
I3W1_coeff2 = ((-2)*(mw1=10)) + ((-1)*((mw1=8)+(mw1=9)+(mw1=11)+(mw1=12))) + ((1)*((mw1=2)+(mw1=3)+(mw1=5)+(mw1=6))) + ((2)*(mw1=4))
I3W1_coeff3 = ((-2)*(mw1=6)) + ((-1)*((mw1=4)+(mw1=5)+(mw1=7)+(mw1=8))) + ((1)*((mw1=0)+(mw1=1)+(mw1=2)+(mw1=10)+(mw1=11))) + ((2)*(mw1=12))
I3W1C = scale1*(I3W1_coeff1*IAW1.phasor+I3W1_coeff2*IBW1.phasor+I3W1_coeff3*ICW1.phasor)*(1/(TAP1*CTR1)) 

#WINDING 2

scale2 = (mw2=0)*1 + ((mw2=1)+(mw2=3)+(mw2=5)+(mw2=7)+(mw2=9)+(mw2=11))*(1/sqrt(3)) + ((mw2=2)+(mw2=4)+(mw2=6)+(mw2=8)+(mw2=10)+(mw2=12))*(1/3)

I1W2_coeff1 = ((-2)*(mw2=6)) + ((-1)*((mw2=4)+(mw2=5)+(mw2=7)+(mw2=8))) + ((1)*((mw2=0)+(mw2=1)+(mw2=2)+(mw2=10)+(mw2=11))) + ((2)*(mw2=12))
I1W2_coeff2 = ((-2)*(mw2=2)) + ((-1)*((mw2=1)+(mw2=3)+(mw2=4)+(mw2=12))) + ((1)*((mw2=6)+(mw2=7)+(mw2=9)+(mw2=10))) + ((2)*(mw2=8))
I1W2_coeff3 = ((-2)*(mw2=10)) + ((-1)*((mw2=8)+(mw2=9)+(mw2=11)+(mw2=12))) + ((1)*((mw2=2)+(mw2=3)+(mw2=5)+(mw2=6))) + ((2)*(mw2=4))
I1W2C = scale2*(I1W2_coeff1*IAW2.phasor+I1W2_coeff2*IBW2.phasor+I1W2_coeff3*ICW2.phasor)*(1/(TAP2*CTR2)) 

I2W2_coeff1 = ((-2)*(mw2=10)) + ((-1)*((mw2=8)+(mw2=9)+(mw2=11)+(mw2=12))) + ((1)*((mw2=2)+(mw2=3)+(mw2=5)+(mw2=6))) + ((2)*(mw2=4))
I2W2_coeff2 = ((-2)*(mw2=6)) + ((-1)*((mw2=4)+(mw2=5)+(mw2=7)+(mw2=8))) + ((1)*((mw2=0)+(mw2=1)+(mw2=2)+(mw2=10)+(mw2=11))) + ((2)*(mw2=12))
I2W2_coeff3 = ((-2)*(mw2=2)) + ((-1)*((mw2=1)+(mw2=3)+(mw2=4)+(mw2=12))) + ((1)*((mw2=6)+(mw2=7)+(mw2=9)+(mw2=10))) + ((2)*(mw2=8))
I2W2C = scale2*(I2W2_coeff1*IAW2.phasor+I2W2_coeff2*IBW2.phasor+I2W2_coeff3*ICW2.phasor)*(1/(TAP2*CTR2)) 

I3W2_coeff1 = ((-2)*(mw2=2)) + ((-1)*((mw2=1)+(mw2=3)+(mw2=4)+(mw2=12))) + ((1)*((mw2=6)+(mw2=7)+(mw2=9)+(mw2=10))) + ((2)*(mw2=8))
I3W2_coeff2 = ((-2)*(mw2=10)) + ((-1)*((mw2=8)+(mw2=9)+(mw2=11)+(mw2=12))) + ((1)*((mw2=2)+(mw2=3)+(mw2=5)+(mw2=6))) + ((2)*(mw2=4))
I3W2_coeff3 = ((-2)*(mw2=6)) + ((-1)*((mw2=4)+(mw2=5)+(mw2=7)+(mw2=8))) + ((1)*((mw2=0)+(mw2=1)+(mw2=2)+(mw2=10)+(mw2=11))) + ((2)*(mw2=12))
I3W2C = scale2*(I3W2_coeff1*IAW2.phasor+I3W2_coeff2*IBW2.phasor+I3W2_coeff3*ICW2.phasor)*(1/(TAP2*CTR2)) 

IRT1 = (mag(I1W1C)+mag(I1W2C))
IRT2 = (mag(I2W1C)+mag(I2W2C))
IRT3 = (mag(I3W1C)+mag(I3W2C))
IOP1 = mag(I1W1C+I1W2C)
IOP2 = mag(I2W1C+I2W2C)
IOP3 = mag(I3W1C+I3W2C)